import { _decorator, Node, Sprite } from 'cc';
import {DeskInfo} from "db://assets/script/game/desk/view/deskInfoView";
import {smc} from "db://assets/script/game/common/singletonModuleComp";
import {PlayerView} from "db://assets/script/game/majiang/view/playerView";
import {api} from "db://assets/script/game/player/api";
import {openDialogConfirm} from "db://assets/script/game/common/config/gameUiConfig";
import {ecs} from "db://oops-framework/libs/ecs/ECS";
import {CCComp} from "db://oops-framework/module/common/CCComp";
import {WheelView} from "db://assets/script/game/desk/view/wheelView";
const { ccclass, property } = _decorator;

@ccclass('MjDeskView')
@ecs.register("MjDeskView")
export class MjDeskView extends CCComp {

    reset(): void {
    }

    @property({
        type: Node,
        tooltip: "规则展示框"
    })
    optShow: Node

    @property({
        type: Node,
        tooltip: "准备按钮"
    })
    btnReady: Node

    @property({
        type: Sprite,
        tooltip: "背景"
    })
    bg: Sprite

    @property({
        type: Node,
        tooltip: "player layer"
    })
    allPlayer: Node

    @property({
        type: Node,
        tooltip: "player layer"
    })
    direction: Node

    // start() {
    // }

    // 更新建房规则
    updateOptShow() {
        this.optShow.active = true
        const view = this.optShow.getComponent(DeskInfo)
        view.updateDeskNo(smc.gameMj.deskInfo.deskNo)
        view.updateJuShu(smc.gameMj.deskInfo.juShu.toString())
        view.updateDiFen(smc.gameMj.deskInfo.diFen.toString())
        view.updateTitle(smc.gameMj.categoryConf.itemName)
    }

    // 禁止规则牌
    hideOptShow() {
        this.optShow.active = false
    }

    getPlayerView() {
        return this.allPlayer.getComponent(PlayerView)
    }

    // 玩家准备好了
    playerReady() {
        // 隐藏准备按钮
        this.btnReady.active = false
    }

    // 开始倒计时
    startCounterByShortId(shortId: number) {
        const view = this.direction.getComponent(WheelView)
        view.startCounter(shortId, 60)
    }

    bindCounterByShortId(shortId: number, pos: number) {
        const view = this.direction.getComponent(WheelView)
        view.bindCounter(shortId, pos)
    }

    resetPlayerCounter() {
        const view = this.direction.getComponent(WheelView)
        view.resetPlayer()
    }

    // 退出房间
    clickExitDesk() {
        if (!smc.gameMj.deskInfo.mjOpt.isRuby) {
            // TODO 不是金豆房,如果是房主，弹窗提示退出会解散房间
            console.error("not ruby desk")
        } else {
            // 提示退出会被托管
            const okFunc = () => {
                api.deskExit(null, () => {
                    // 显示大厅
                    smc.dating.show()
                })
            }
            openDialogConfirm("提示", "退出将被托管", okFunc)
        }
    }

    clickReady() {
        // 玩家准备
        api.deskReady({ isReady: true }, () => {
            this.playerReady()
            this.getPlayerView().toggleReadyHandByIndex(0, true)
        })
    }
}


